package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.*;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_GROUP, desc = "分组对阵信息")
public class ChannelEnterNameGroupCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        EnterNameProto.ChannelEnterNameGroupReqMsg reqMsg = EnterNameProto.ChannelEnterNameGroupReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();
        EnterNameProto.EnterNameGroupRespMsg.Builder clientMsg = EnterNameProto.EnterNameGroupRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setType(type);
        EnterNameModel model = ChannelEnterNameMgr.getEnterNameModelMap().get(activityId);
        EnterNameProto.EnterNameGroupListMsg.Builder listMsg = EnterNameProto.EnterNameGroupListMsg.newBuilder();
        listMsg.setActivityId(activityId);
        if(model != null){
            UserEnterName enterName = model.getUserEnterName(reqMsg.getUserKey().getUserId());
            int groupId = 0;
            if(enterName != null) {
                groupId = enterName.getGroupId();
            }
            if(groupId > 0) {
                ActivityGroupMatch groupMatch = model.getActivityGroupMatch(groupId);
                if(groupMatch != null) {
                    EnterNameProto.EnterNameGroupMsg.Builder builder = EnterNameProto.EnterNameGroupMsg.newBuilder();
                    builder.setGroupOne(groupMatch.getGroupId());
                    builder.setGroupTow(groupMatch.getEnemyGroupId());
                    builder.addAllZoneOne(groupMatch.getZoneIdList());
                    builder.setGroupOneVal("0");
                    builder.setGroupTowVal("0");
                    EnterNameGroupRankModel rankModel = ChannelEnterNameRankMgr.getEnterNameGroupRankModel(activityId, type, groupMatch.getRankMatchGroupId());
                    if(rankModel != null){
                        EnterNameGroupRank groupRank = rankModel.getMyRankInfo(groupMatch.getGroupId());
                        if(groupRank != null){
                            builder.setGroupOneVal(groupRank.getValue().toString());
                        }
                    }
                    if(groupMatch.getEnemyGroupId() > 0){
                        ActivityGroupMatch enemy = model.getActivityGroupMatch(groupMatch.getEnemyGroupId());
                        builder.addAllZoneTwo(enemy.getZoneIdList());
                        if(rankModel != null){
                            EnterNameGroupRank groupRank = rankModel.getMyRankInfo(groupMatch.getEnemyGroupId());
                            if(groupRank != null) {
                                builder.setGroupTowVal(groupRank.getValue().toString());
                            }
                        }
                    }
                    listMsg.addGroup(builder);
                }
            }
        }
        clientMsg.setList(listMsg);
        ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_GROUP, clientMsg, reqMsg.getUserKey().getChannelId(),
                reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());

    }

    @Override
    public void sendData() {

    }
}
